package com.midian.service.sys.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.model.sys.SysConf;
import com.midian.service.common.BaseService;
import com.midian.service.sys.ISysConfService;

/**
 * 系统配置业务实现类
 * @author Mengfh
 *
 */
@Service("sysConfService")
public class SysConfServiceImpl extends BaseService implements ISysConfService {
	/**系统配置DAO*/
	private BaseDaoTemplet<SysConf> sysConfDAO;
	
	@Resource(name = "sysConfDAO")
	public void setSysConfDAO(BaseDaoTemplet<SysConf> sysConfDAO) {
		this.sysConfDAO = sysConfDAO;
	}

	@Override
	public SysConf add(SysConf target) throws BizException {
		this.sysConfDAO.add(target);
		return target;
	}

	@Override
	public void update(SysConf target) throws BizException {
		this.sysConfDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.sysConfDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.sysConfDAO.delete(ids);
	}

	@Override
	public SysConf get(Serializable id) throws BizException {
		return this.sysConfDAO.get(id);
	}

	@Override
	public List<SysConf> showList(SysConf target, Pager pager, List<OrderHelper> helper) throws BizException {
		return this.sysConfDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.sysConfDAO.opDelFlag(ids, status);
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = {};
		Type[] types = {};
		return SearchHelper.getSearchList(attrs, types);
	}

	@Override
	public List<SysConf> showAllSysConfList(String code) {
		StringBuilder hql = new StringBuilder("from SysConf o where o.parentId is not null ");
		List<Object> values = new ArrayList<Object>();
		if(StringUtils.isNotBlank(code)){
			hql.append(" and o.code = ?");
			values.add(code);
		}
		hql.append(" order by o.createTime desc");
		return this.sysConfDAO.find(hql.toString(), values.toArray());
	
	}

	@Override
	public SysConf getSysConfByCode(String code) throws BizException {
		String hql = "from SysConf o where o.code = ?";
		return (SysConf) this.sysConfDAO.findObject(hql, new Object[]{code});
	}
	
	
	
}
